<?php defined('BASEPATH') OR exit('No direct script access allowed');


class Vvc extends Web_Controller{

    public function __construct() 
    {
        parent::__construct();

		$this->load->model('bases/verify_code_model');
    }


	//生成数字图片验证码
    public function index(){

		$width=200;
		$height=50;
		
		$image=imagecreatetruecolor($width,$height);
		imagesetthickness($image,1);
		imagealphablending($image,true);
		$color_black=imagecolorallocatealpha($image,0,0,0,0);
		$color_black_semi=imagecolorallocatealpha($image,0,0,0,115);
		$color_white=imagecolorallocatealpha($image,255,255,255,0);
		imagefill($image,0,0,$color_white);
		imagecolortransparent($image,$color_white);
		
		$acceptedCharsA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$acceptedCharsB = '0123456789';
		$password = '';
		$stringlength = 4;
		for ($i = 0; $i < $stringlength; $i++)
		{
			$password .= substr($acceptedCharsB, mt_rand(0, strlen($acceptedCharsB) - 1), 1);
		}

		//制造混淆字符	
		for($i=0;$i<50;$i++){
			$color=imagecolorallocatealpha($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),110);
			imagestring($image, mt_rand(1,6), mt_rand(-$width*0.25,$width*1.25), mt_rand(-$height*0.25,$height*1.25), $acceptedCharsA{mt_rand(0,strlen($acceptedCharsA)-1)}, $color);
			imagestring($image, mt_rand(1,6), mt_rand(-$width*0.25,$width*1.25), mt_rand(-$height*0.25,$height*1.25), $acceptedCharsB{mt_rand(0,strlen($acceptedCharsB)-1)}, $color);
		}

		//添加验证码到图片		
		for($i=0;$i<$stringlength;$i++){
			$buffer=imagecreatetruecolor(40,40);
			imagefill($buffer,0,0,$color_white);
			imagecolortransparent($buffer,$color_white);
			
			$buffer2=imagecreatetruecolor(40,40);
			imagefill($buffer2,0,0,$color_white);
			imagecolortransparent($buffer2,$color_white);
			
			$red=0;$green=0;$blue=0;
			while ($red+$green+$blue<400||$red+$green+$blue>450){
				$red = mt_rand(0,255);
				$green = mt_rand(0,255);
				$blue = mt_rand(0,255);
			}
			
			$color=imagecolorallocate($buffer,$red,$green,$blue);
			imagestring($buffer,5,0,0,substr($password,$i,1),$color);
			
			imagecopyresized($buffer2,$buffer,0,0,0,0,mt_rand(30,40),mt_rand(30,40),10,14);
			$buffer=imagerotate($buffer2,mt_rand(-10,10),$color_white);
			
			$xpos=$i/$stringlength*($width-20)+(($width-30)/$stringlength/2)-6;
			$ypos=(($height-50)/2)+5;
			
			imagecolortransparent($buffer,$color_white);
			
			imagecopymerge($image,$buffer,$xpos,$ypos,0,0,imagesx($buffer),imagesy($buffer),100);
			imagedestroy($buffer);
			imagedestroy($buffer2);
		}

		//以下均是添加混淆		
		for($i=0;$i<12;$i++){
			$color=imagecolorallocatealpha($image,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),110);
			imagefilledellipse($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(10,40),mt_rand(10,40),$color);
		}
		
		for($i=0;$i<12;$i++){
			$color=imagecolorallocatealpha($image,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),110);
			imagesetthickness($image,mt_rand(8,20));
			imageline($image,mt_rand(-$width*0.25,$width*1.25),mt_rand(-$height*0.25,$height*1.25),
			mt_rand(-$width*0.25,$width*1.25),mt_rand(-$height*0.25,$height*1.25), $color);  
			imagesetthickness($image,1);
		}
		
		$sindivide=mt_rand(1,20);
		$sinwidth=mt_rand(1,$sindivide)/4;
		for ($i=0;$i<$height;$i++){
			$buffer=imagecreatetruecolor($width,1);
			imagecopy($buffer,$image,0,0,0,$i,$width,1);
			imageline($image,0,$i,$width,$i,$color_white);
			imagecopy($image,$buffer,(sin($i/$sindivide)-.5)*2*$sinwidth,$i,0,0,$width,1);
			imagedestroy($buffer);
		}
		
		$sindivide=mt_rand(1,20);
		$sinwidth=mt_rand(1,$sindivide)/4;
		for ($i=0;$i<$width;$i++){
			$buffer=imagecreatetruecolor(1,$height);
			imagecopy($buffer,$image,0,0,$i,0,1,$height);
			imageline($image,$i,0,$i,$width,$color_white);
			imagecopy($image,$buffer,$i,(sin($i/$sindivide)-.5)*2*$sinwidth,0,0,1,$height);
			imagedestroy($buffer);
		}
		
		$red_from=mt_rand(0,255);	$red_to=mt_rand(0,255);
		$green_from=mt_rand(0,255);	$green_to=mt_rand(0,255);
		$blue_from=mt_rand(0,255);	$blue_to=mt_rand(0,255);
		
		for ($i=0;$i<$height;$i++){
			$color=imagecolorallocatealpha($image,$red_from+($red_to-$red_from)/$height*$i,$green_from+($green_to-$green_from)/$height*$i,$blue_from+($blue_to-$blue_from)/$height*$i,100);
			imageline($image,0,$i,$width,$i,$color);
		}
		
		$red_from=mt_rand(0,255);	$red_to=mt_rand(0,255);
		$green_from=mt_rand(0,255);	$green_to=mt_rand(0,255);
		$blue_from=mt_rand(0,255);	$blue_to=mt_rand(0,255);
		
		for ($i=0;$i<$width;$i++){
			$color=imagecolorallocatealpha($image,$red_from+($red_to-$red_from)/$width*$i,$green_from+($green_to-$green_from)/$width*$i,$blue_from+($blue_to-$blue_from)/$width*$i,100);
			imageline($image,$i,0,$i,$height,$color);
		}

        $vvc_id = intval($_GET['id']);
        $vvc_token = trim($this->input->get('token', true));

		//将新生成的图形验证码保存到数据库
		$result = $this->verify_code_model->save_verify_code($vvc_id, $vvc_token, $password);


		header("Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
		header('Content-Type: image/png');

		imagepng($image);
		imagedestroy($image);
		
	}
}	

?>